#1 导入依赖
import requests
from lxml import etree

#2 发送请求和获取响应
html = requests.get('https://www.dushu.com/lianzai/1134.html')
print(html.status_code)
html.encoding='utf-8'
html_text = html.text
# print(html_text)


#3 解析html文件，返回一个节点树的对象
html_selector = etree.HTML(html_text)

#4 正则/Xpath/bs4 三选一 分析自己想要的数据
#4 获取根节点html的元素
# (1)获取图片
imgs = html_selector.xpath("/html/body/div[6]/div/div[2]/div[2]/ul/li/div/div/a/img/@data-original")
# print(len(imgs))
# for img in imgs:
#     print(img)


# (2)书名
booknames = html_selector.xpath("/html/body/div[6]/div/div[2]/div[2]/ul/li/div/h3/a/text()")
# print(type(bookname))  #<class 'list'>
print(len(booknames))
# for i in bookname:
    # print(i)
#（3）作者名
# authors = html_selector.xpath("/html/body/div[6]/div/div[2]/div[2]/ul/li/div/p[1]/*[contains(text(), '<a ')]")  #作废
authors = html_selector.xpath("/html/body/div[6]/div/div[2]/div[2]/ul/li/div/p[1]")
# 对有（美）、（英）的作者要另外判断
# if 如果有a标签，那么xpath要加上a标签，如果没有a标签，那么xpath就和目前保持一致
# 执行XPath查询
print(authors)
# 检查结果
if len(authors) > 0:
    print(authors)
    print("存在包含a标签的元素")
else:
    print("不存在包含a标签的元素")
    print(authors)
#
# todo 这是第2种解法
# authors2=[]
# for i in authors:
#     # print(i.xpath("./a/text()"))
#     # print(i.xpath("./a"))
#     if i.xpath("./a"):
#         # print("存在a标签")
#         # print(i.xpath("./text()"))
#         if i.xpath("./text()"): #如果除了a标签，其他内容不为空，则有[0]
#             print(i.xpath("./text()")[0] + i.xpath("./a/text()")[0])
#             authors2.append(i.xpath("./text()")[0] + i.xpath("./a/text()")[0])
#         else:  #如果除了a标签，其他内容为空，则只需要提取a标签内容
#             print(i.xpath("./a/text()")[0])
#             authors2.append(i.xpath("./a/text()")[0])
#
#     else:
#         print("不存在a标签")
#         # print(i.xpath("./text()")[0])
#         authors2.append(i.xpath("./text()")[0])
#
# print(len(authors2))  #判断是否为40，如果是40个，那么数量就对了

# print(authors)
#（4）书籍描述
descriptions = html_selector.xpath("/html/body/div[6]/div/div[2]/div[2]/ul/li/div/p[2]/text()")
print(len(descriptions))

# # 5 把内容通过zip组装
# zipped=list(zip(imgs,booknames,authors2,descriptions))
#
# # print(type(zipped)) #<class 'list'>
# #
# for i in zipped:
#     print(i)
#
# #6 把内容保存在文件里
# with open("./xuanhuan.txt", 'w', encoding='utf-8') as file:
#     for book in zipped:
#         file.write(book[0]+",  "+book[1]+",  "+book[2]+",  "+book[3]+"\n")
#
#


